﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace Pst_Backup_Agent
{
    internal class ApplicationSettings
    {
        internal enum SourceSettings
        {
            GPO,
            Local
        }

        #region private fields

        private Utilities logger = new Utilities();
        private SourceSettings _readSettingsFrom = SourceSettings.Local;
        private string _baseKey = string.Empty;

        private string _clientID = string.Empty;

        private bool _bckAgtDontBackupThroughtWan = true;
        private string _bckAgtAdditionalSubnets = string.Empty;
        private string _bckAgtLogoPath = string.Empty;
        private bool _bckAgtSetExclusiveNTFSPermissions = false;
        private string _bckAgtAdditionalNTFSFullcontrol = String.Empty;
        private string _bckAgtAdditionalNTFSReadWrite = String.Empty;

        private bool _eventLogActivated = true;
        private string _eventLogseverity = "Information";

        private bool _filesandFoldersCompressFiles = false;
        private string _filesAndFoldersDestinationPath = string.Empty;

        private string _reportingServer = string.Empty;

        private string _schedulePolicy = "Every";
        private int _scheduleEveryInterval = 7;
        private string _scheduleEveryUnit = "Days";
        private int _scheduleMonthlyDay = 15;
        private int _scheduleWeeklyDay = 0;

        #endregion private fields

        private ApplicationSettings() { }

        /// <summary>
        /// Constructor for the class.
        /// </summary>
        /// <param name="readSettingsFrom">Where to read registry values. Can be either 'GPO', so values will be read from "Software\Policies\PST Backup 2013", or 'Local', so values will read from "Software\PST Backup 2013".</param>        
        internal ApplicationSettings(SourceSettings readSettingsFrom)
        {
            this._readSettingsFrom = readSettingsFrom;
            this.logger.IsLogActivated = true;
            this.logger.LogSeverity = "Debug";
            IsBackupAgentDontBackupThroughtWanDefine = false;
            IsBackupAgentAdditionalSubnetsDefine = false;

            IsEventLogSeverityDefine = false;

            IsFilesAndFoldersCompressFilesDefine = false;
            IsFilesAndFoldersDestinationPathDefine = false;
            
            IsSchedulePolicyDefine = false;
            IsScheduleEveryintervalDefine = false;
            IsScheduleEveryUnitDefine = false;
            IsScheduleMonthlyDayDefine = false;
            IsScheduleWeeklyDayDefine = false;

            switch (this._readSettingsFrom)
            {
                case SourceSettings.GPO:
                    this._baseKey = @"Software\Policies\PST Backup 2013";
                    break;
                case SourceSettings.Local:
                    this._baseKey = @"Software\PST Backup 2013";
                    break;
                default:
                    break;
            }
            this.LoadSettingsFromRegistry();
        }

        #region intenal properties

        internal string ClientID { get { return this._clientID; } set { this._clientID = value; } }

        internal bool BackupAgentDontBackupThroughtWan { get { return this._bckAgtDontBackupThroughtWan; } set { this._bckAgtDontBackupThroughtWan = value; } }
        internal string BackupAgentAdditionalSubnets { get { return this._bckAgtAdditionalSubnets; } set { this._bckAgtAdditionalSubnets = value; } }
        internal string BackupAgentLogoPath { get { return this._bckAgtLogoPath; } set { this._bckAgtLogoPath = value; } }
        internal bool BackupAgentSetExclusiveNTFSPermissions { get { return this._bckAgtSetExclusiveNTFSPermissions; } set { this._bckAgtSetExclusiveNTFSPermissions = value; } }
        internal string BackupAgentAdditionalNTFSFullcontrol { get { return this._bckAgtAdditionalNTFSFullcontrol; } set { this._bckAgtAdditionalNTFSFullcontrol = value; } }
        internal string BackupAgentAdditionalNTFSReadWrite { get { return this._bckAgtAdditionalNTFSReadWrite; } set { this._bckAgtAdditionalNTFSReadWrite = value; } }

        internal bool EventLogActivated { get { return this._eventLogActivated; } set { this._eventLogActivated = value; } }
        internal string EventLogSeverity { get { return this._eventLogseverity; } set { this._eventLogseverity = value; } }

        internal bool FilesAndFoldersCompressFiles { get { return _filesandFoldersCompressFiles; } set { _filesandFoldersCompressFiles = value; } }
        internal string FilesAndFoldersDestinationPath { get { return this._filesAndFoldersDestinationPath; } set { this._filesAndFoldersDestinationPath = value; } }

        internal string ReportingServer { get { return this._reportingServer; } set { this._reportingServer = value; } }

        internal string SchedulePolicy { get { return this._schedulePolicy; } set { this._schedulePolicy = value; } }
        internal int ScheduleEveryInterval { get { return this._scheduleEveryInterval; } set { this._scheduleEveryInterval = value; } }
        internal string ScheduleEveryUnit { get { return this._scheduleEveryUnit; } set { this._scheduleEveryUnit = value; } }
        internal int ScheduleMonthlyDay { get { return this._scheduleMonthlyDay; } set { this._scheduleMonthlyDay = value; } }
        internal int ScheduleWeeklyDay { get { return this._scheduleWeeklyDay; } set { this._scheduleWeeklyDay = value; } }
                
        internal bool IsBackupAgentDontBackupThroughtWanDefine { get; set; }
        internal bool IsBackupAgentAdditionalSubnetsDefine { get; set; }

        internal bool IsEventLogSeverityDefine { get; set; }

        internal bool IsFilesAndFoldersCompressFilesDefine { get; set; }
        internal bool IsFilesAndFoldersDestinationPathDefine { get; set; }
         
        internal bool IsSchedulePolicyDefine { get; set; }
        internal bool IsScheduleEveryintervalDefine { get; set; }
        internal bool IsScheduleEveryUnitDefine { get; set; }
        internal bool IsScheduleMonthlyDayDefine { get; set; }
        internal bool IsScheduleWeeklyDayDefine { get; set; }

        #endregion intenal properties

        #region internal Methodes

        internal void OverrideLocalSettingsWithGPOSettings(ApplicationSettings gpoSettings)
        {

            if (gpoSettings.IsBackupAgentDontBackupThroughtWanDefine)
                this.BackupAgentDontBackupThroughtWan = gpoSettings.BackupAgentDontBackupThroughtWan;
            if (gpoSettings.IsBackupAgentAdditionalSubnetsDefine)
                this.BackupAgentAdditionalSubnets = gpoSettings.BackupAgentAdditionalSubnets;
            if (!string.IsNullOrEmpty(gpoSettings.BackupAgentLogoPath))
                this.BackupAgentLogoPath = gpoSettings.BackupAgentLogoPath;
            this.BackupAgentSetExclusiveNTFSPermissions = gpoSettings.BackupAgentSetExclusiveNTFSPermissions;
            this.BackupAgentAdditionalNTFSFullcontrol = gpoSettings.BackupAgentAdditionalNTFSFullcontrol;
            this.BackupAgentAdditionalNTFSReadWrite = gpoSettings.BackupAgentAdditionalNTFSReadWrite;
            
            if (gpoSettings.IsEventLogSeverityDefine)
                this.EventLogSeverity = gpoSettings.EventLogSeverity;

            if (gpoSettings.IsFilesAndFoldersCompressFilesDefine)
                this.FilesAndFoldersCompressFiles = gpoSettings.FilesAndFoldersCompressFiles;
            if (gpoSettings.IsFilesAndFoldersDestinationPathDefine)
                this.FilesAndFoldersDestinationPath = gpoSettings.FilesAndFoldersDestinationPath;

            if (!string.IsNullOrEmpty(gpoSettings.ReportingServer))
                this.ReportingServer = gpoSettings.ReportingServer;

            if (gpoSettings.IsSchedulePolicyDefine)
                this.SchedulePolicy = gpoSettings.SchedulePolicy;
            if (gpoSettings.IsScheduleEveryintervalDefine)
                this.ScheduleEveryInterval = gpoSettings.ScheduleEveryInterval;
            if (gpoSettings.IsScheduleEveryUnitDefine)
                this.ScheduleEveryUnit = gpoSettings.ScheduleEveryUnit;
            if (gpoSettings.IsScheduleMonthlyDayDefine)
                this.ScheduleMonthlyDay = gpoSettings.ScheduleMonthlyDay;
            if (gpoSettings.IsScheduleWeeklyDayDefine)
                this.ScheduleWeeklyDay = gpoSettings.ScheduleWeeklyDay;
        }

        internal void SaveLocalSettings()
        {
            try
            {
                RegistryKey baseKey = Registry.CurrentUser.OpenSubKey(_baseKey, true);

                if (baseKey == null)
                    baseKey =this.CreateBaseKey();
                RegistryKey settingsKey = baseKey.OpenSubKey("Settings", true);
                if (settingsKey == null)
                    settingsKey = this.CreateLocalSettingsKeys(baseKey);

                this.SaveStringValue(settingsKey, "Backup Agent", "DontBackupThroughWan", this.BackupAgentDontBackupThroughtWan.ToString());
                this.SaveStringValue(settingsKey, "Backup Agent", "AdditionalSubnets", this.BackupAgentAdditionalSubnets.ToString());
                this.SaveStringValue(settingsKey, "Backup Agent", "LogoPath", this.BackupAgentLogoPath);

                this.SaveStringValue(settingsKey, "Event Log", "Severity", this.EventLogSeverity.ToString());
                this.SaveStringValue(settingsKey, "Event Log", "LogEvent", this.EventLogActivated.ToString());

                this.SaveStringValue(settingsKey, "Files And Folders", "CompressFiles", this.FilesAndFoldersCompressFiles.ToString());
                this.SaveStringValue(settingsKey, "Files And Folders", "DestinationPath", this.FilesAndFoldersDestinationPath.ToString());

                this.SaveStringValue(settingsKey, "Reporting", "Server", this.ReportingServer.ToString());

                this.SaveStringValue(settingsKey, "Schedule", "Policy", this.SchedulePolicy.ToString());

                this.SaveIntValue(settingsKey, @"Schedule\Every", "Interval", this.ScheduleEveryInterval);
                this.SaveStringValue(settingsKey, @"Schedule\Every", "Unit", this.ScheduleEveryUnit);

                this.SaveIntValue(settingsKey, @"Schedule\Monthly", "Day", this.ScheduleMonthlyDay);

                this.SaveIntValue(settingsKey, @"Schedule\Weekly", "Day", this.ScheduleWeeklyDay);
            }
            catch (Exception) { }
        }

        #endregion internal Methods 

        #region private methods

        private void LoadSettingsFromRegistry()
        {
            try
            {
                RegistryKey baseKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    baseKey = Registry.CurrentUser.OpenSubKey(_baseKey, true);
                else
                    baseKey = Registry.CurrentUser.OpenSubKey(_baseKey, false);

                if (baseKey == null)
                    baseKey = this.CreateBaseKey();
                if (baseKey != null)
                {
                    if (this._readSettingsFrom == SourceSettings.Local)
                    {
                        this.ClientID = this.GetClientID(baseKey);
                    }

                    RegistryKey settingsKey;
                    if (this._readSettingsFrom == SourceSettings.Local)
                        settingsKey = baseKey.OpenSubKey("Settings", true);
                    else
                        settingsKey = baseKey.OpenSubKey("Settings", false);

                    if (settingsKey == null)
                        settingsKey = this.CreateLocalSettingsKeys(baseKey);

                    if (settingsKey != null)
                    {
                        this.LoadBackupAgentSettings(settingsKey);
                        this.LoadEventLogSettings(settingsKey);
                        this.LoadFilesAndFoldersSettings(settingsKey);
                        this.LoadReportingSettings(settingsKey);
                        this.LoadScheduleSettings(settingsKey);

                        settingsKey.Close();
                    }
                    baseKey.Close();
                }
            }
            catch (Exception ex)
            {
                logger.LogEvent(20002, "An error occure while reading " + (this._readSettingsFrom == SourceSettings.Local ? "Local" : "GPO") + @" Settings from HKCU\" + _baseKey + ".\r\n" + ex.Message,2, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        private RegistryKey CreateBaseKey()
        {
            RegistryKey baseKey = null;
            if (this._readSettingsFrom == SourceSettings.Local)
            {
                try
                {
                    baseKey = Registry.CurrentUser.CreateSubKey(_baseKey);
                    this.CreateLocalSettingsKeys(baseKey);
                }
                catch (Exception ex) { logger.LogEvent(20003, @"An error occurs while creating base Keys in HKCU\" + _baseKey + ".\r\n" + ex.Message,2, System.Diagnostics.EventLogEntryType.Error); }
            }
            return baseKey;
        }

        private string GetClientID(RegistryKey baseKey)
        {
            string clientID = baseKey.GetValue("ClientID", string.Empty).ToString();
            if (string.IsNullOrEmpty(clientID))
            {
                clientID = Guid.NewGuid().ToString();
                this.CreateMissingStringValue(baseKey, "ClientID", clientID);
            }

            return clientID;
        }

        private RegistryKey CreateLocalSettingsKeys(RegistryKey baseKey)
        {
            RegistryKey settingsKey = null;
            if (this._readSettingsFrom == SourceSettings.Local)
            {
                try
                {
                    baseKey.CreateSubKey(@"Settings\Files And Folders");
                    baseKey.CreateSubKey(@"Settings\Schedule");
                    baseKey.CreateSubKey(@"Settings\Schedule\Every");
                    baseKey.CreateSubKey(@"Settings\Schedule\Weekly");
                    baseKey.CreateSubKey(@"Settings\Schedule\Monthly");
                    baseKey.CreateSubKey(@"Settings\Event Log");
                    baseKey.CreateSubKey(@"Settings\Reporting");
                    baseKey.CreateSubKey(@"Settings\Backup Agent");

                    settingsKey = baseKey.OpenSubKey("Settings");
                }
                catch (Exception ex) { logger.LogEvent(20004, @"An error occurs while creating Local Settings Keys in HKCU\" + _baseKey + ".\r\n" + ex.Message,2, System.Diagnostics.EventLogEntryType.Error); }
            }
            return settingsKey;
        }

        private void LoadBackupAgentSettings(RegistryKey settingsKey)
        {
            try
            {
                RegistryKey bckAgtKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    bckAgtKey = settingsKey.OpenSubKey("Backup Agent", true);
                else
                    bckAgtKey = settingsKey.OpenSubKey("Backup Agent", false);

                if (bckAgtKey == null)
                    bckAgtKey = this.CreateMissingKey(settingsKey, "Backup Agent");

                if (bckAgtKey != null)
                {
                    object data = null;
                    
                    data = bckAgtKey.GetValue("DontBackupThroughWan", null);
                    if (data == null)
                        this.CreateMissingStringValue(bckAgtKey, "DontBackupThroughWan", this.BackupAgentDontBackupThroughtWan.ToString());
                    else
                    {
                        this.BackupAgentDontBackupThroughtWan = (data.ToString().ToLower() == "true" ? true : false);
                        this.IsBackupAgentDontBackupThroughtWanDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    data = bckAgtKey.GetValue("AdditionalSubnets", null);
                    if (data == null)
                        this.CreateMissingStringValue(bckAgtKey, "AdditionalSubnets", this.BackupAgentAdditionalSubnets.ToString());
                    else
                    {
                        this.BackupAgentAdditionalSubnets = data.ToString();
                        this.IsBackupAgentAdditionalSubnetsDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    data = bckAgtKey.GetValue("LogoPath", null);
                    if (data == null)
                        this.CreateMissingStringValue(bckAgtKey, "LogoPath", this.BackupAgentLogoPath.ToString());
                    else
                    {
                        this.BackupAgentLogoPath = data.ToString();
                    }

                    data = bckAgtKey.GetValue("SetExclusiveNTFSPermissions", null);
                    if (data == null)
                        this.BackupAgentSetExclusiveNTFSPermissions = false;
                    else
                    {
                        this.BackupAgentSetExclusiveNTFSPermissions = (data.ToString().ToLower() == "true" ? true : false);
                    }

                    data = bckAgtKey.GetValue("AdditionalNTFSFullcontrol", null);
                    if (data == null)
                        this.BackupAgentAdditionalNTFSFullcontrol = String.Empty;
                    else
                    {
                        this.BackupAgentAdditionalNTFSFullcontrol = data.ToString();
                    }

                    data = bckAgtKey.GetValue("AdditionalNTFSReadWrite", null);
                    if (data == null)
                        this.BackupAgentAdditionalNTFSReadWrite = String.Empty;
                    else
                    {
                        this.BackupAgentAdditionalNTFSReadWrite = data.ToString();
                    }                    

                    bckAgtKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadEventLogSettings(RegistryKey settingsKey)
        {
            try
            {
                RegistryKey eventLogKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    eventLogKey = settingsKey.OpenSubKey("Event Log", true);
                else
                    eventLogKey = settingsKey.OpenSubKey("Event Log", false);

                if (eventLogKey == null)
                    eventLogKey = this.CreateMissingKey(settingsKey, "Event Log");

                if (eventLogKey != null)
                {
                    object data = null;

                    data = eventLogKey.GetValue("LogEvent", null);
                    if(data == null)
                    {
                        this.CreateMissingStringValue(eventLogKey, "LogEvent", this.EventLogActivated.ToString());
                    }
                    else
                    {
                        this.EventLogActivated = (data.ToString().ToLower() == "true" ? true : false);
                    }

                    data = eventLogKey.GetValue("Severity", null);
                    if (data == null)
                        this.CreateMissingStringValue(eventLogKey, "Severity", this.EventLogSeverity.ToString());
                    else
                    {
                        this.EventLogSeverity = data.ToString();
                        this.IsEventLogSeverityDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }
                    eventLogKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadFilesAndFoldersSettings(RegistryKey settingsKey)
        {
            try
            {
                RegistryKey filesAndFoldersKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    filesAndFoldersKey = settingsKey.OpenSubKey("Files and Folders", true);
                else
                    filesAndFoldersKey = settingsKey.OpenSubKey("Files and Folders", false);

                if (filesAndFoldersKey == null)
                    filesAndFoldersKey = this.CreateMissingKey(settingsKey, "Files and Folders");

                if (filesAndFoldersKey != null)
                {
                    object data = null;

                    data = filesAndFoldersKey.GetValue("CompressFiles", null);
                    if (data == null)
                        this.CreateMissingStringValue(filesAndFoldersKey, "CompressFiles", this.FilesAndFoldersCompressFiles.ToString());
                    else
                    {
                        this.FilesAndFoldersCompressFiles = (data.ToString().ToLower() == "true" ? true : false);
                        this.IsFilesAndFoldersCompressFilesDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    data = filesAndFoldersKey.GetValue("DestinationPath", null);
                    if (data == null)
                        this.CreateMissingStringValue(filesAndFoldersKey, "DestinationPath", this.FilesAndFoldersDestinationPath.ToString());
                    else
                    {
                        this.FilesAndFoldersDestinationPath = data.ToString();
                        this.IsFilesAndFoldersDestinationPathDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    filesAndFoldersKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadReportingSettings(RegistryKey settingsKey)
        {
            try
            {
                RegistryKey reportingKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    reportingKey = settingsKey.OpenSubKey("Reporting", true);
                else
                    reportingKey = settingsKey.OpenSubKey("Reporting", false);

                if (reportingKey == null)
                    reportingKey = this.CreateMissingKey(settingsKey, "Reporting");

                if (reportingKey != null)
                {
                    object data = null;
                   
                    data = reportingKey.GetValue("Server", null);
                    if (data == null)
                        this.CreateMissingStringValue(reportingKey, "Server", this.ReportingServer.ToString());
                    else
                    {
                        this.ReportingServer = data.ToString();
                    }

                    reportingKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadScheduleSettings(RegistryKey settingsKey)
        {
            try
            {
                RegistryKey scheduleKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    scheduleKey = settingsKey.OpenSubKey("Schedule", true);
                else
                    scheduleKey = settingsKey.OpenSubKey("Schedule", false);

                if (scheduleKey == null)
                    scheduleKey = this.CreateMissingKey(settingsKey, "Schedule");

                if (scheduleKey != null)
                {
                    object data = null;
                    data = scheduleKey.GetValue("Policy", null);
                    if (data == null)
                        this.CreateMissingStringValue(scheduleKey, "Policy", this.SchedulePolicy.ToString());
                    else
                    {
                        this.SchedulePolicy = data.ToString();
                        this.IsSchedulePolicyDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    this.LoadScheduleEverySettings(scheduleKey);
                    this.LoadScheduleMonthlySettings(scheduleKey);
                    this.LoadScheduleWeeklySettings(scheduleKey);

                    scheduleKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadScheduleEverySettings(RegistryKey scheduleKey)
        {
            try
            {
                RegistryKey everyKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    everyKey = scheduleKey.OpenSubKey("Every", true);
                else
                    everyKey = scheduleKey.OpenSubKey("Every", false);

                if (everyKey == null)
                    everyKey = this.CreateMissingKey(everyKey, "Every");

                if (everyKey != null)
                {
                    object data = null;
                    data = everyKey.GetValue("Interval", null);
                    if (data == null)
                        this.CreateMissingIntValue(everyKey, "Interval", this.ScheduleEveryInterval);
                    else
                    {
                        this.ScheduleEveryInterval = Utilities.GetCorrectIntValue((int)data, 1, 365, 7);
                        this.IsScheduleEveryintervalDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    data = everyKey.GetValue("Unit", null);
                    if (data == null)
                        this.CreateMissingStringValue(everyKey, "Unit", this.ScheduleEveryUnit.ToString());
                    else
                    {
                        this.ScheduleEveryUnit = data.ToString();
                        this.IsScheduleEveryUnitDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }
                    everyKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadScheduleMonthlySettings(RegistryKey scheduleKey)
        {
            try
            {
                RegistryKey monthlyKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    monthlyKey = scheduleKey.OpenSubKey("Monthly", true);
                else
                    monthlyKey = scheduleKey.OpenSubKey("Monthly", false);

                if (monthlyKey == null)
                    monthlyKey = this.CreateMissingKey(monthlyKey, "Monthly");

                if (monthlyKey != null)
                {
                    object data = null;
                    data = monthlyKey.GetValue("Day", null);
                    if (data == null)
                        this.CreateMissingIntValue(monthlyKey, "Day", this.ScheduleMonthlyDay);
                    else
                    {
                        this.ScheduleMonthlyDay = Utilities.GetCorrectIntValue((int)data, 1, 31, 15);
                        this.IsScheduleMonthlyDayDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    monthlyKey.Close();
                }
            }
            catch (Exception) { }
        }

        private void LoadScheduleWeeklySettings(RegistryKey scheduleKey)
        {
            try
            {
                RegistryKey weeklyKey;
                if (this._readSettingsFrom == SourceSettings.Local)
                    weeklyKey = scheduleKey.OpenSubKey("Weekly", true);
                else
                    weeklyKey = scheduleKey.OpenSubKey("Weekly", false);

                if (weeklyKey == null)
                    weeklyKey = this.CreateMissingKey(scheduleKey, "Weekly");

                if (weeklyKey != null)
                {
                    object data = null;
                    data = weeklyKey.GetValue("Day", null);
                    if (data == null)
                        this.CreateMissingIntValue(weeklyKey, "Day", this.ScheduleWeeklyDay);
                    else
                    {
                        this.ScheduleWeeklyDay = Utilities.GetCorrectIntValue((int)data, 0, 6, 0);
                        this.IsScheduleWeeklyDayDefine = this._readSettingsFrom == SourceSettings.GPO;
                    }

                    weeklyKey.Close();
                }
            }
            catch (Exception) { }
        }

        private RegistryKey CreateMissingKey(RegistryKey settingsKey, string keyName)
        {
            if (this._readSettingsFrom == SourceSettings.Local)
            {
                return settingsKey.CreateSubKey(keyName);
            }
            return null;
        }

        private void CreateMissingStringValue(RegistryKey keyName, string valueName, string data)
        {
            if (this._readSettingsFrom == SourceSettings.Local)
            {
                keyName.SetValue(valueName, data, RegistryValueKind.String);
            }
        }

        private void CreateMissingIntValue(RegistryKey keyName, string valueName, int data)
        {
            if (this._readSettingsFrom == SourceSettings.Local)
            {
                keyName.SetValue(valueName, data, RegistryValueKind.DWord);
            }
        }

        private void SaveStringValue(RegistryKey settingsKey, string keyName, string valueName, string data)
        {
            try
            {
                RegistryKey key = settingsKey.OpenSubKey(keyName, true);
                if (key == null)
                    key = this.CreateMissingKey(settingsKey, keyName);
                key.SetValue(valueName, data, RegistryValueKind.String);
            }
            catch (Exception) { }
        }

        private void SaveIntValue(RegistryKey settingsKey, string keyName, string valueName, int data)
        {
            try
            {
                RegistryKey key = settingsKey.OpenSubKey(keyName, true);
                if (key == null)
                    key = this.CreateMissingKey(settingsKey, keyName);
                key.SetValue(valueName, data, RegistryValueKind.DWord);
            }
            catch (Exception) { }
        }

        #endregion private methods
    }
}
